package com.c2b.algorithm.newcoder;

/**
 * <a href='https://www.nowcoder.com/practice/fcf87540c4f347bcb4cf720b5b350c76?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj'>寻找峰值 </ahref=''>
 *
 * @author c2b
 * @since 2024/3/14 13:26
 */
public class BM19FindPeakElement {
    public static class Solution {
        public int findPeakElement (int[] nums) {
            int left = 0;
            int right = nums.length - 1;
            while (left < right) {
                int mid = left + ((right - left) >> 1);
                // 说明midIndex右侧是向下的趋势。此时不一定有波峰。需要向左查找
                if (nums[mid] > nums[mid + 1]) {
                    right = mid;
                }
                // 说明midIndex右侧是向上的趋势。此时一定有波峰
                else {
                    left = mid + 1;
                }
            }
            return right;
        }
    }
}
